<?php

namespace App\Http\Controllers\Api;

use App\Permission;
use App\Role;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Zizaco\Entrust\Entrust;

class RolesController extends BaseController
{
    public $user;

    public function __construct(Request $request)
    {
        parent::__construct();
        $this->user = $this->is_login($request);
    }
    //

    /**
     * @param Request $request
     * 查询角色信息
     */
    public function info(Request $request){

        if (!$this->user['data']->can('roles_manager')){
            return error('NoPermission');
        }

        $user_type = $this->user['data']['type'];
        $cinema_code = $this->user['data']['cinema_code'];

        if (!$id = $request->id){
            return error('角色id缺失');
        }

        if (!$info = Role::where(['cinema_code'=>$cinema_code,'type'=>$user_type])->find($id)){
            return error('该角色不存在');
        }

        $permission = \DB::table('permission_role')->where(['role_id'=>$id])->pluck('permission_id');
        $info['permission'] = $permission;
        return success($info,'查询成功');
    }

    /*
     * 查询角色
     */
    public function index(Request $request){

        if (!$this->user['data']->can('roles_manager')){
            return error('NoPermission');
        }

        $user_type = $this->user['data']['type'];
        $cinema_code = $this->user['data']['cinema_code'];
        $status = $request->status;
        $data = \DB::table('roles')->where(['type'=>$user_type,'cinema_code'=>$cinema_code])->where('status','!=',0);
        $status&&$data->where('status','=',$status);
        $list = $data->get();
        foreach ($list as $key=>$val){
            $info = \DB::table('permission_role')->where('permission_role.role_id','=',$val->id)
                ->join('permissions','permissions.id','=','permission_role.permission_id')
                ->pluck('permissions.display_name');
            $list[$key]->permissions_name = $info;
        }
        return success($list,'查询成功');
    }

    /**
     * 编辑角色
     */
    public function edit(Request $request){

        if (!$this->user['data']->can('roles_manager')){
            return error('NoPermission');
        }

        $user_type = $this->user['data']['type'];
        $cinema_code = $this->user['data']['cinema_code'];
        if (!$id = $request->id){
            return error('角色id缺失');
        }
        $role_info = Role::where(['type'=>$user_type,'status'=>1,'cinema_code'=>$cinema_code])->find($id);
        if (!$role_info){
            return error('该角色不存在');
        }

        if (!$name = $request->name){
            return error('角色名称缺失!');
        }

        $count = Role::where(['type'=>$user_type,'name'=>$name,'cinema_code'=>$cinema_code])->where('id','!=',$id)->count();
        if ($count>0){
            return error('该角色已存在');
        }

        if (!$permission_ids = $request->permission_ids){
            return error('权限不能为空');
        }
        $permission_ids = explode(',',$permission_ids);
        $description = $request->description;

        $role_info->name = $name;
        $role_info->type = $user_type;
        $role_info->cinema_code = $cinema_code;
        $role_info->description = $description;

        \DB::beginTransaction();
        try{
            $res_delete_role_permission = \DB::table('permission_role')->where(['role_id'=>$role_info->id])->delete();
            $res_set_role = $role_info->save();
            if ($res_delete_role_permission&&$res_set_role&&$role_info->perms()->sync($permission_ids)){
                \DB::commit();
                logs($this->user,'编辑角色'.$role_info->name.'成功');
                return success($role_info->id,'编辑成功');
            }
        }catch (\Exception $e){
            \DB::rollBack();
            logs($this->user,'编辑角色'.$role_info->name.'失败');
            return error('编辑失败');
        }

    }

    /**
     * 删除角色
     */
    public function delete(Request $request){

        if (!$this->user['data']->can('roles_manager')){
            return error('NoPermission');
        }

        $user_type = $this->user['data']['type'];
        $cinema_code = $this->user['data']['cinema_code'];
        if (!$id = $request->id){
            return error('角色id缺失');
        }
        $arr = explode(',',$id);
        $role_info = Role::where(['type'=>$user_type,'status'=>1,'cinema_code'=>$cinema_code])->whereIn('id',$arr)->get();
        if (count($arr)!=$role_info->count()){
            return error('该角色不存在');
        }

        $count = \DB::table('role_user')->whereIn('role_id',$arr)->count();
        if ($count>0){
            return error('该角色已存在绑定用户，不可禁用');
        }

        $res_delete_role = \DB::table('roles')->whereIn('id',$arr)->update(['status'=>2]);
        if ($res_delete_role){
            logs($this->user,'禁用角色'.array_get($role_info,'name').'成功');
            return success('','角色禁用成功');
        }
        return error('角色禁用失败');

    }

    /**
     * @param Request $request
     * 恢复角色
     */
    public function restore_roles(Request $request){

        if (!$this->user['data']->can('roles_manager')){
            return error('NoPermission');
        }

        $user_type = $this->user['data']['type'];
        $cinema_code = $this->user['data']['cinema_code'];
        if (!$id = $request->id){
            return error('角色id缺失');
        }
        $arr = explode(',',$id);
        $role_info = Role::where(['type'=>$user_type,'status'=>2,'cinema_code'=>$cinema_code])->whereIn('id',$arr)->get();
        if (count($arr)!=$role_info->count()){
            return error('角色不可恢复');
        }
        $res_restore_role = \DB::table('roles')->whereIn('id',$arr)->update(['status'=>1]);
        if ($res_restore_role){
            logs($this->user,'恢复角色'.array_get($role_info,'name').'成功');
            return success('','角色恢复成功');
        }
        return error('角色恢复失败');

    }

    /**
     * 添加角色
     */
    public function add(Request $request){

        if (!$this->user['data']->can('roles_manager')){
            return error('NoPermission');
        }


        $user_type = $this->user['data']['type'];
        $cinema_code = $this->user['data']['cinema_code'];
        if (!$name = $request->name){
            return error('角色名称缺失!');
        }


        $count = Role::where(['type'=>$user_type,'name'=>$name,'cinema_code'=>$cinema_code])->count();
        if ($count>0){
            return error('该角色已存在');
        }


        if (!$permission_ids = $request->permission_ids){
            return error('权限不能为空');
        }
        $permission_ids = explode(',',$permission_ids);

        $description = $request->description;

        $role = new Role();
        $role->name = $name;
        $role->type = $user_type;
        $role->cinema_code = $cinema_code;
        $role->description = $description;
        \DB::beginTransaction();
        try{
            if($role->save()&&$role->perms()->sync($permission_ids)){
                \DB::commit();
                logs($this->user,'添加角色'.$role->name.'成功');
                return success($role->id,'角色添加成功');
            }
        }catch (\Exception $e){
            \DB::rollBack();
            logs($this->user,'添加角色'.$role->name.'失败');
            return error('角色添加失败');
        }

    }

    /**
     * 查询所有权限
     */
    public function permission(Request $request){

        if (!$this->user['data']->can('roles_manager')){
            return error('NoPermission');
        }

        $user_type = $this->user['data']['type'];
        $list =  Permission::join('permission_role','permission_role.permission_id','=','permissions.id')
            ->join('roles','roles.id','=','permission_role.role_id')
            ->where(['roles.status'=>0,'roles.type'=>$user_type])
            ->select('permissions.id','permissions.pid','permissions.name','permissions.display_name','permissions.description')
            ->get();

        return success($list,'查询成功');
    }
}
